/*
 * @Descripttion: your project
 * @version: 1.0
 * @Author: hongweixun
 * @Date: 2023-05-17 13:50:32
 * @LastEditors: liyin 2179253556@qq.com
 * @LastEditTime: 2025-08-29 15:32:10
 */
import { defineConfig } from 'vite'
import path from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import postcssPxToViewport from 'postcss-px-to-viewport'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import vue from '@vitejs/plugin-vue'
const times = new Date().getTime()
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue(), AutoImport({
    resolvers: [ElementPlusResolver()],
  }),
  Components({
    resolvers: [ElementPlusResolver({
      importStyle: "sass"
    })],
    directoryAsNamespace: true
  }),
  ],
  resolve: {
    alias: {
      // 如果报错__dirname找不到，需要安装node,npm add @types/node --save-dev
      "@": path.resolve(__dirname, "src"),
      "coms": path.resolve(__dirname, "src/components"),
    }
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "@/assets/element/index.scss" as *;`,
      },
    },
    postcss:{
      plugins:[
        postcssPxToViewport({
          viewportWidth: 1920,   //---基于1200宽度为100vw
          viewportHeight: 1080   //---基于1080宽度为100vh
        })
      ]
    }
  },
  base: './',
  server: {
    // 服务器主机名，如果允许外部访问，可设置为"0.0.0.0"
    host: '0.0.0.0',
    // 服务器端口号
    port: 6020,
    // boolean | string 启动项目时自动在浏览器打开应用程序；如果为string，比如"/index.html"，会打开http://localhost:3000/index.html
    open: false,
    // 自定义代理规则
    proxy: {
      '/api': {
        // target: 'http://192.168.80.8:60100',
        target: 'https://prepay.hlzhjy.com/api',
        // target: 'https://mall.hlzhjy.com',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  },
  build: {
    // minify: 'terser',
    // 指定输出路径，默认'dist'
    outDir: 'dist',
    // 指定生成静态资源的存放路径(相对于build.outDir)
    assetsDir: 'assets',
    // 小于此阈值的导入或引用资源将内联为base64编码，设置为0可禁用此项。默认4096（4kb）
    assetsInlineLimit: 4096,
    // 启用/禁用CSS代码拆分，如果禁用，整个项目的所有CSS将被提取到一个CSS文件中,默认true
    cssCodeSplit: true,
    // 构建后是否生成source map文件，默认false
    sourcemap: false,
    // 为true时，会生成manifest.json文件，用于后端集成
    manifest: false,
    rollupOptions: {
      output: {
         // chunkFileNames: "static/js/[name]-[hash].js",
         entryFileNames: `assets/js/[name]-[hash]${times}.js`,
         assetFileNames: `assets/[ext]/[name]-[hash]${times}.[ext]`,
         // 静态资源分拆打包
         manualChunks(id) {
             if (id.includes('node_modules')) {
                 return id.toString().split('node_modules/')[1].split('/')[0].toString()
             }
         },
         // 打包时候根据文件名分包
         chunkFileNames: (chunkInfo) => {
             const facadeModuleId = chunkInfo.facadeModuleId
                 ? chunkInfo.facadeModuleId.split('/')
                 : [];
             const fileName =
                 facadeModuleId[facadeModuleId.length - 2] || '[name]';
             return `js/${fileName}/[name].[hash].${times}.js`;
         }
      },
    },
    // 生产环境移除console
    terserOptions: {
      compress: {
          drop_console: true,
          drop_debugger: true
      }
  }
  }
})